package com.soulkun.smarthome.ui.main.home

import androidx.fragment.app.viewModels
import androidx.navigation.navGraphViewModels
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainHomeWeatherBinding
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeViewModel
import com.soulkun.smarthome.ui.main.home.viewmodel.AppMainHomeWeatherViewModel
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.logE
import soulkun.library.utils.time.TimeUtils

class AppMainHomeWeatherFragment : BaseDataBindingFragment<AppFragmentMainHomeWeatherBinding>() {

    private val homeViewModel by lazy {
        requireParentFragment().navGraphViewModels<AppMainHomeViewModel>(R.id.app_navigation_main_home).value
    }
    private val viewModel by viewModels<AppMainHomeWeatherViewModel> { defaultViewModelProviderFactory }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_home_weather
    }

    override fun initView() {

    }

    override fun initData() {
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
    }

    override fun initListener() {
        homeViewModel.selectFamilyLiveData.observe(viewLifecycleOwner) { familyVO ->
            if (familyVO != null) {
                viewModel.showStatusLiveData.value = false
                viewModel.getFamilyLocationWeather(familyVO.latitude, familyVO.longitude)
                    .observe(viewLifecycleOwner) { result ->
                        result.getOrNull()?.let {
                            viewModel.showStatusLiveData.value = true
                            viewModel.weatherLiveData.value = it
                            when (it.skycon) {
                                "CLEAR_DAY" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_clear_day
                                    )
                                    requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                        R.drawable.app_main_home_weather_background_clear_day
                                    )
                                }
                                "CLEAR_NIGHT" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_clear_night
                                    )
                                    requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                        R.drawable.app_main_home_weather_background_clear_night
                                    )
                                }
                                "PARTLY_CLOUDY_DAY" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_partly_cloudy_day
                                    )
                                    requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                        R.drawable.app_main_home_weather_background_partly_cloudy_day
                                    )
                                }
                                "PARTLY_CLOUDY_NIGHT" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_partly_cloudy_night
                                    )
                                    requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                        R.drawable.app_main_home_weather_background_partly_cloudy_night
                                    )
                                }
                                "CLOUDY" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_cloudy
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_cloudy_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_cloudy_night
                                        )
                                    }
                                }
                                "LIGHT_HAZE" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_light_haze
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_night
                                        )
                                    }
                                }
                                "MODERATE_HAZE" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_moderate_haze
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_night
                                        )
                                    }
                                }
                                "HEAVY_HAZE" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_heavy_haze
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_night
                                        )
                                    }
                                }
                                "LIGHT_RAIN" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_light_rain
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_night
                                        )
                                    }
                                }
                                "MODERATE_RAIN" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_moderate_rain
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_night
                                        )
                                    }
                                }
                                "HEAVY_RAIN" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_heavy_rain
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_night
                                        )
                                    }
                                }
                                "STORM_RAIN" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_storm_rain
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_rain_night
                                        )
                                    }
                                }
                                "FOG" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_light_haze
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_fog_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_fog_night
                                        )
                                    }
                                }
                                "LIGHT_SNOW" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_light_snow
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_night
                                        )
                                    }
                                }
                                "MODERATE_SNOW" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_moderate_snow
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_night
                                        )
                                    }
                                }
                                "HEAVY_SNOW" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_heavy_snow
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_night
                                        )
                                    }
                                }
                                "STORM_SNOW" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_storm_snow
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_snow_night
                                        )
                                    }
                                }
                                "DUST" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_sand
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_night
                                        )
                                    }
                                }
                                "SAND" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_sand
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_haze_night
                                        )
                                    }
                                }
                                "WIND" -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_wind
                                    )
                                    if (TimeUtils.isDayOrNight()) {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_wind_day
                                        )
                                    } else {
                                        requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                            R.drawable.app_main_home_weather_background_wind_night
                                        )
                                    }
                                }
                                else -> {
                                    requireDataBinding().ivMainHomeWeatherIconWeather.setImageResource(
                                        R.drawable.app_main_home_weather_skyicon_partly_cloudy_day
                                    )
                                    requireDataBinding().clMainHomeWeatherLayoutWeather.setBackgroundResource(
                                        R.drawable.app_main_home_weather_background_partly_cloudy_day
                                    )
                                }
                            }
                        }
                    }
            }
        }
    }

    override fun doOnCreate() {
    }

}